#!/usr/bin/env python
#coding=UTF-8
'''
Created on 2010-11-5

@author: zarra
噪声产生器
'''

from random import random
from numpy.random import normal
from numpy import pi,exp,sqrt
import cv

from common import copy_image

class Noise_Gen(object):
    
    def __init__(self):pass
        
    
    def generator(self): 
        pass
    
    def __call__(self,image):
        self.image =  copy_image(image)
        self.generator()
        return self.image
    
    
class Gaussian(Noise_Gen):
    def __init__(self,m=0,v=1):
        super(self.__class__,self).__init__()
        self.m=m
        self.v=v
    
    def generator(self):
        self.normal = normal(self.m,self.v,self.image.width*self.image.height)
        for y in range(self.image.height):
            for x in range(self.image.width):
                        self.image[y,x]+=self.normal[y*self.image.height+x]
        
        
        
class Salt_Pepper(Noise_Gen):
    def __init__(self,s=0.02,p=0.02):
        super(self.__class__,self).__init__()
        self.s=s
        self.p=p
        
    def generator(self):
        for y in range(self.image.height):
            for x in range(self.image.width):
                if random() <self.s:
                    self.image[y,x] = 255
                if random() < self.p:
                    self.image[y,x] = 0
                    
        
        